<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要bb的内容（最多吐槽120字）" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,index) in cmtlist" :key="item.id">
                <div class="cmt-tile">
                    第{{index+1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间:{{item.add_time | dateFormat}}
                </div>
                <div class="cmt-body">
                    {{item.evaluation}}
                </div>
            </div>

            <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
        </div>
        
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
export default {
    data(){
        return {
            page:1,
            cmtlist:[],
            msg:''
        }
    },
    created(){
        this.getCmtList()
    },
    methods:{
        //获取评价列表
        getCmtList(){
            this.$http.get("news/cmtlist/"+this.id+"?page="+this.page).then(
                response => {
                    if (response.body.status === 0) {
                        // this.cmtlist = response.body.message;
                          this.cmtlist = this.cmtlist.concat(response.body.message)
                    }
                }
            );
        },
        //评价列表加载更多
        getmore(){
            this.page++
            this.getCmtList()
        },
        // 提交评论
        postComment(){
            if(this.msg.length===0){
                return Toast('评论不能为空');
            }
            this.$http.post('news/postComment/'+this.id, {evaluation:this.msg.trim()}).then(result=>console.log(result));
            var cmt = {user_name:'匿名用户',add_time:Date.now(),evaluation:this.msg.trim()}
            this.cmtlist.unshift(cmt)
            this.msg = ''
        }
    },
    props:[
        "id"
    ]
}
</script>

<style lang="scss" scoped>
    .cmt-container {
        h3 {
            font-size: 18px;
        }
        textarea {
            height: 85px;
            font-size: 14px;
            margin: 0;
        }

        .cmt-list {
            margin: 5px 0;
            .cmt-item{
                font-size: 14px;
                .cmt-tile{
                    background-color: #ccc;
                    line-height: 30px;
                }
                .cmt-body {
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
        
    }
</style>
